import React, { useState,useEffect } from 'react'
import PubSub from 'pubsub-js';
export default function Item () {
  const [list, setList] = useState([])
  function delItem (index) {
    let newList = list.filter((_, idx) => index !== idx)
    setList(newList)
  }
  useEffect(() => {
    console.log('@')
    let a = PubSub.subscribe('addItem', (_,data) => {
      setList(list => [data,...list])
    });
    return () => {
      PubSub.unsubscribe(a)
    }
  }, [])
 
  return (
      list.map((item,index) => <li key = {index} onClick = { () => delItem(index) }>{item}</li>)
  )
}